package ru.tpu.AAD.MalakhovaZaripov.DAO;

import org.hibernate.Session;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import ru.tpu.AAD.MalakhovaZaripov.Entities.Bill;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Category;
import ru.tpu.AAD.MalakhovaZaripov.Entities.ComplexItem;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Country;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Customer;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Device;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Employee;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Mfr;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Post;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Service;
import ru.tpu.AAD.MalakhovaZaripov.Entities.StockItem;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Supplier;

public class ComputerShopDAO {
	
	private SessionFactory sessionFactory;

	public ComputerShopDAO() {
		AnnotationConfiguration aconf = new AnnotationConfiguration();
		aconf.addAnnotatedClass(Country.class);
		aconf.addAnnotatedClass(Mfr.class);
		aconf.addAnnotatedClass(Device.class);
		aconf.addAnnotatedClass(Category.class);
		aconf.addAnnotatedClass(Bill.class);
		aconf.addAnnotatedClass(Service.class);
		aconf.addAnnotatedClass(Employee.class);
		aconf.addAnnotatedClass(Supplier.class);
		aconf.addAnnotatedClass(Post.class);
		aconf.addAnnotatedClass(Customer.class);
		aconf.addAnnotatedClass(ComplexItem.class);
		aconf.addAnnotatedClass(StockItem.class);
		Configuration conf = aconf.configure();
	    sessionFactory = conf.buildSessionFactory();
	}
	
	public SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}
	
	public List<Category> getAllCategories(Session session)
	{
		List<Category> allCategories =  (ArrayList<Category>)session.createCriteria(Category.class).list();
		return allCategories;
	}
	
	public Category getCategoryById(Integer id, Session session)
	{
		try
		{
		Category category = (Category)session.get(Category.class,id);
		return category;
		}
		catch (Exception ex)
		{
			return null;
		}
	}
	
	public Category getCategoryByName(String categoryName, Session session)
	{
		try
		{
		Query categoryByName = session.createQuery("from Category category where category.categoryName = :name");
		categoryByName.setString("name", categoryName);
		Category category = (Category) categoryByName.list().get(0); 
		return category;
		}
		catch (Exception ex)
		{
			return null;
		}
	}

	public void insertCustomer(int id, String name, String address, String phone, String fax, String email) {
		Session session1 = sessionFactory.openSession();
	   	Customer cust = new Customer();
		cust.setCustomerId(id);
		cust.setCustomerAddress(address);
		cust.setCustomerEmail(email);
		cust.setCustomerFax(fax);
		cust.setCustomerName(name);
		cust.setCustomerPhone(phone);
		session1.beginTransaction();
		session1.save(cust);
		session1.getTransaction().commit();
		session1.close();	    
	}
	public void insertCustomer(Customer cust) {
		Session session1 = sessionFactory.openSession();
		session1.beginTransaction();
		session1.save(cust);
		session1.getTransaction().commit();
		session1.close();
	}
	public void updateCustomer (Customer cust) {
		Session session1 = sessionFactory.openSession();
		session1.beginTransaction();
		session1.saveOrUpdate(cust);
		session1.getTransaction().commit();
		session1.close();
	}
	
	public void deleteCustomer (Customer cust) {
		Session session1 = sessionFactory.openSession();
		session1.beginTransaction();
		session1.delete(cust);
		session1.getTransaction().commit();
		session1.close();
	
	}
	
	
}
